package com.shuaiche.yeguan.config;


import com.shuaiche.yeguan.SCApplication;
import com.shuaiche.yeguan.model.SCMerchantDetailModel;
import com.shuaiche.yeguan.model.SCUserInfoResponse;
import com.shuaiche.yeguan.util.SPUtils;

/**
 * 用户信息配置
 */
public class SCUserInfoConfig {

    /**
     * 保存用户信息信息
     *
     * @param loginResponseData
     */
    public static void saveUserinfo(SCUserInfoResponse loginResponseData) {
        SCApplication.getApplication().setUserinfoModel(loginResponseData);

        SPUtils.saveBean(SCApplication.getApplication(),SPUtils.USER_INFO,loginResponseData);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static SCUserInfoResponse getUserinfo() {
        SCUserInfoResponse userinfoModel = SCApplication.getApplication().getUserinfoModel();
        if (userinfoModel == null) {
            userinfoModel = SPUtils.getBean(SCApplication.getApplication(),SPUtils.USER_INFO,SCUserInfoResponse.class);
            SCApplication.getApplication().setUserinfoModel(userinfoModel);
        }
        return userinfoModel;
    }


    /**
     * 删除用户信息用户信息
     *
     * @return
     */
    public static void deteleUserInfo() {
        SCApplication.getApplication().setUserinfoModel(null);
        SPUtils.remove(SCApplication.getApplication(),SPUtils.USER_INFO);
    }

    public static boolean isJoinUnion() {
        if (isLogin()) {
            SCUserInfoResponse userinfoModel = getUserinfo();
            if (userinfoModel.getScUnions() != null && userinfoModel.getScUnions().size() > 0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 企业是否是经纪人
     *
     * @return
     */
    public static boolean isMerchantBroker() {
        if (isLogin()) {
            SCUserInfoResponse userinfoModel = getUserinfo();
            if (userinfoModel.getIsMerchantBroker() != null && userinfoModel.getIsMerchantBroker() == 1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 个人经纪人
     *
     * @return
     */
    public static boolean isUserBroker() {
        if (isLogin()) {
            SCUserInfoResponse userinfoModel = getUserinfo();
            if (userinfoModel.getIsUserBroker() != null && userinfoModel.getIsUserBroker() == 1) {
                return true;
            }
        }
        return false;
    }


    public static void saveCompanyInfo(SCMerchantDetailModel model) {
        SCApplication.getApplication().setCompanyInfoModel(model);
        SPUtils.saveBean(SCApplication.getApplication(),SPUtils.COMPANY_INFO,model);
    }

    public static SCMerchantDetailModel getCompanyInfo() {
        SCMerchantDetailModel company = SCApplication.getApplication().getCompanyInfo();
        if (company == null) {
            company = SPUtils.getBean(SCApplication.getApplication(),SPUtils.COMPANY_INFO,SCMerchantDetailModel.class);
            SCApplication.getApplication().setCompanyInfoModel(company);
        }
        return company;
    }

    /**
     * 删除用户信息用户信息
     *
     * @return
     */
    public static void deteleCompanyInfo() {
        SCApplication.getApplication().setCompanyInfoModel(null);
        SPUtils.remove(SCApplication.getApplication(),SPUtils.COMPANY_INFO);
    }


    public static void deleteMessageInfo() {

    }

    /**
     * 判断用户是否登入
     *
     * @return
     */
    public static boolean isLogin() {
        return getUserinfo() != null;
    }


    /**
     * 只有1是通过
     * 状态   null: 未填写, 0:注册公司， 1:审核通过, 2:审核不通过,3:冻结， 4:审核中
     *
     * @return
     */
    public static boolean isCheckPass() {
        if (isLogin()) {
            if (getUserinfo().getMerchantStatus() != null && getUserinfo().getMerchantStatus() == 1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否已经注册了公司 0：注册
     *
     * @return
     */
    public static boolean isRegisterMerchant() {
        if (isLogin()) {
            if (getUserinfo().getMerchantStatus() != null) {
                return true;
            }
        }

        return false;
    }


    /**
     * 是否诚信联盟 联盟会员
     *
     * @return
     */
    public static boolean isHornorUnions() {
        if (isLogin()) {
            if (getUserinfo().getScUnions() != null) {
                for (int i = 0; i < getUserinfo().getScUnions().size(); i++) {
                    if (2 == getUserinfo().getScUnions().get(i).getUnionType()) {
                        return true;
                    }
                }
            }

//            if (getUserinfo().getIsOfficialMember() != null && getUserinfo().getIsOfficialMember() == 1) {
//                return true;
//            }
        }
        return false;
    }

    /**
     * 是否官方联盟 且 认证通过
     *
     * @return
     */
    public static boolean isOfficeUnionAndAuth() {
        if (isLogin()) {
            if (getUserinfo().getIsOfficialMember() != null &&
                    getUserinfo().getIsOfficialMember() == 1 && isCheckPass()) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否官方联盟
     *
     * @return
     */
    public static boolean isOfficeUnion() {
        if (isLogin()) {
            if (getUserinfo().getIsOfficialMember() != null &&
                    getUserinfo().getIsOfficialMember() == 1) {
                return true;
            }
        }
        return false;
    }


    /**
     * 官方经纪人（一级经纪人）
     *
     * @return
     */
    public static boolean isOfficeBroker() {
        if (isLogin()) {
            if (getUserinfo().getBrokerChannel() != null &&
                    getUserinfo().getBrokerAuditStatus() != null &&
                    getUserinfo().getBrokerAuditStatus() == 2 &&
                    (getUserinfo().getBrokerChannel() == 1 || getUserinfo().getBrokerChannel() == 2)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 经纪人是否冻结
     *
     * @return
     */
    public static boolean isBrokerFreeze() {
        if (isLogin()) {
            if (getUserinfo().getBrokerAuditStatus() != null && getUserinfo().getAccountStatus() == 2) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否是商户邀请的经纪人（二级经纪人）
     *
     * @return
     */
    public static boolean isMerchantInviteBroker() {
        if (isLogin()) {
            if (getUserinfo().getBrokerChannel() != null &&
                    getUserinfo().getBrokerAuditStatus() != null && getUserinfo().getBrokerAuditStatus() == 2 &&
                    getUserinfo().getBrokerChannel() == 3) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否隔离  1：隔离  0：未隔离
     *
     * @return
     */
    public static boolean IsQuarantine() {
        if (isLogin()) {
            if (getUserinfo().getIsQuarantine() != null &&
                    getUserinfo().getIsQuarantine() == 1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否是企业经纪人
     *
     * @return
     */
    public static boolean isMerchantBrokerType() {
        if (isLogin()) {
            if (getUserinfo().getBrokerType() != null && getUserinfo().getBrokerType() == 2) {
                return true;
            }
        }
        return false;
    }


    /**
     * 是否是品牌门店
     *
     * @return
     */
    public static boolean isScBrand() {
        if (getUserinfo() != null && getUserinfo().getIsScBrand() != null && getUserinfo().getIsScBrand() == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 是否是保证金商家
     *
     * @return
     */
    public static boolean isIsMarginMerchant() {
        if (isLogin() && getCompanyInfo() != null && getCompanyInfo().getIsMarginMerchant() == 1) {
            return true;
        }
        return false;
    }

    /**
     * 是否是 官方经销商 并且被冻结
     *
     * @return
     */
    public static boolean isOfficeBrokerBrokerFreeze() {
        SCMerchantDetailModel companyInfo = getCompanyInfo();
        if (isLogin() && companyInfo != null) {
            if (companyInfo.getAccountStatus() == 2) {
                return true;
            }
        }
        return false;
//        return true;
    }

    /**
     * 获取当前会员等级
     */
    public static int getMemberClass() {
        if (isLogin() && getUserinfo() != null) {
            return getUserinfo().getMemberClass();
        }
        return 0;
    }

    // 会员有效期
    public static boolean getExpireTime() {
        if (isLogin() && getUserinfo() != null && getUserinfo().getExpireTime() != null &&
                getUserinfo().getExpireTime() > 0) {
            return true;
        }
        return false;
    }

    // 是收费会员
    public static boolean isPayMoneyMember() {
        return getMemberClass() >= 20 && getExpireTime();
    }

    /**
     * 是否是严选会员
     */
    public static boolean isStrictlyMember() {
        return getMemberClass() == 40 && getExpireTime();
    }
}
